<?php

namespace App\Http\Controllers;

use App\Models\Business\Business as BusinessModel;
use App\Models\Product\Cart as CartModel;
use App\Models\Product\Product as ProductModel;

use Illuminate\Support\Facades\Validator;

class Cart extends Controller
{

    // 添加购物车
    public function add()
    {
        $busid = request('busid', 0);
        $nums = request('nums', 0);
        $proid = request('proid', 0);

        $userinfo = BusinessModel::find($busid);

        if (!$userinfo) {
            return error('当前用户不存在');
        }

        $productinfo = ProductModel::find($proid);

        if (!$productinfo) {
            return error('当前商品下架');
        }

        // 判断库存
        $stock = bcsub($productinfo['stock'], $nums);

        if ($stock < 0) {
            return error('当前商品库存不足');
        }

        // 购物车查询是否有添加过
        $cartinfo = CartModel::where([['busid', '=',  $busid], ['proid', '=', $proid]])->first();


        if ($cartinfo) {
            // 计算添加的数量
            $num = bcadd($nums, $cartinfo['nums']);
            // 商品总价
            $total = bcmul($num, $productinfo['price'], 2);

            $data = [
                'nums' => $num,
                'total' => $total
            ];

            $result = CartModel::where(['id' => $cartinfo['id']])->update($data);

            if ($result === false) {
                return error('添加失败');
            } else {
                return success('添加成功');
            }
        } else {
            // 商品总价
            $total = bcmul($nums, $productinfo['price'], 2);

            $data = [
                'busid' => $busid,
                'proid' => $proid,
                'nums' => $nums,
                'price' => $productinfo['price'],
                'total' => $total
            ];

            // 验证规则
            $rules = [
                'busid' => 'required',
                'proid' => 'required',
                'nums' => 'required',
                'price' => 'required',
                'total' => 'required'
            ];

            // 错误提示信息
            $message = [
                'busid.required'     => '用户ID必须填写',
                'proid.required' => '商品ID必须填写',
                'nums.required'  => '商品数量必须填写',
                'price.required'  => '商品价格必须填写',
                'total.required'   => '商品总价必须填写'
            ];

            $validator = Validator::make($data, $rules, $message);

            if ($validator->fails()) {
                // errors 获取所有错误信息
                $errors = $validator->errors();
                // first 第一个错误信息;
                return error($errors->first());
            }

            // 用户表验证器校验数据并且添加
            $result = CartModel::create($data);

            if ($result === FALSE) {
                return  error('添加失败');
            } else {
                return success('添加成功');
            }
        }
    }

    // 购物车列表
    public function index()
    {
        $busid = request('busid', 0);

        // 确认订单传过来的购物车ID
        $ids = request('ids', 0);

        $userinfo = BusinessModel::find($busid);

        if (!$userinfo) {
            return error('当前用户不存在');
        }

        $whereIn[] = ['busid', $busid];

        // 判断查询购物车商品，从购物车或者从商品详情立即购买获取
        $qb = CartModel::query();

        if($ids != 0){
            $ids = explode(',', $ids);
            // 批量查询ID值
            $qb->whereIn('id', $ids);
        }
       
        $data = $qb->with('product')->where('busid', $busid)->orderBy('id', 'desc')->get();
        
        return success('', '', $data);
    }

    // 编辑购物车
    public function edit()
    {
        $busid = request('busid', 0);
        $nums = request('nums', 0);
        $id = request('id', 0);

        $userinfo = BusinessModel::find($busid);

        if (!$userinfo) {
            return error('当前用户不存在');
        }

        $cartinfo = CartModel::find($id);

        if (!$cartinfo) {
            return error('当前购物车商品不存在');
        }

        $productinfo = ProductModel::find($cartinfo['proid']);

        if (!$productinfo) {
            return error('当前商品下架');
        }

        // 判断库存
        $stock = bcsub($productinfo['stock'], $nums);

        if ($stock < 0) {
            return error('当前商品库存不足');
        }

        // 商品总价
        $total = bcmul($nums, $productinfo['price'], 2);

        $data = [
            'nums' => $nums,
            'total' => $total
        ];

        $result = CartModel::where(['id' => $id])->update($data);

        if ($result === false) {
            return error('编辑失败');
        } else {
            return success('编辑成功');
        }
    }

    // 删除购物车
    public function del()
    {
        $busid = request('busid', 0);
        $id = request('id', 0);

        $userinfo = BusinessModel::find($busid);

        if (!$userinfo) {
            return error('当前用户不存在');
        }

        $cartinfo = CartModel::find($id);

        if (!$cartinfo) {
            return error('当前购物车商品不存在');
        }

        $result = CartModel::destroy($id);

        if ($result) {
            return success('删除成功');
        } else {
            return error('删除失败');
        }
    }
}
